require 'fileutils'
require 'qiniu'
require 'digest'
require 'openssl'
require 'base64'
require 'time'
require 'net/http'
require 'uri'
class ForumApi < Grape::API
	WillPaginate.per_page = 10
	version 'v1', using: :path
	format :json
	default_format :json
	formatter :json, Grape::Formatter::Rabl
	resource :forum do
		#### 新建帖子 ####
		desc '新建帖子'
		params do
			optional :user_id, type: String, desc: '用户名'
			optional :content, type: String, desc: '内容'
			optional :media_url, type: String, desc: '图片或视频名称'
		end
		post 'create_post' do
			if params[:user_id].nil?
				{code: "400", msg: "参数错误"}
			else
				post = Post.new
				post.author_id = params[:user_id]
				post.content = params[:content]
				post.media_url = params[:media_url]
				post.publish_status = 1
				post.audit_status = 0
				post.save
				{
					code: "0", 
					msg: "帖子正在审核中",
					post_id: post.id
				}
			end
		end


		#### 查询帖子列表  ####
		desc '查询帖子列表'
		params do
			optional :user_id, type: String, desc: '用户名'
			optional :page, type: String, desc: '页码'
		end
		get 'posts' do
			if params[:user_id].nil?
				{code: "400", msg: "参数错误"}
			else
				if params[:page].nil?
					page = 1
				else
					page = params[:page]
				end
				posts = Post.where("audit_status= 1").paginate(:page => page).order('created_at DESC')
				post_list = []
				posts.each do |po|
					user = User.find(po.author_id)
					user_detail = UserDetail.where(user_id: po.author_id).first
					comments = Comment.where(post_id: po.id)
					comment_list = []
					comments.each do |co|
						cuser = User.find(co.author_id)
						comment_info = Hash.new
						comment_info["id"] = co.id
						comment_info["author_id"] = co.author_id
						comment_info["author_name"] = cuser.name
						comment_info["content"] = co.content
						comment_info["created_at"] = co.created_at.nil? ? "" : co.created_at.strftime("%Y-%m-%d %H:%M:%S")
						comment_list << comment_info
					end
					post_info = Hash.new
					post_info["id"] = po.id
					post_info["author_id"] = po.author_id
					post_info["author_name"] = user.name
					post_info["author_head_img"] = user_detail.head_img_url.nil? ? "" : user_detail.head_img_url
					post_info["content"] = po.content
					post_info["media_url"] = po.media_url.nil? ? "" : po.media_url
					post_info["comment_num"] = comments.count
					post_info["comment_list"] = comment_list
					post_info["created_at"] = po.created_at.nil? ? "" : po.created_at.strftime("%Y-%m-%d %H:%M:%S")
					post_list << post_info
				end
				count = posts.size
				next_page = params[:page].to_i + 1
				{
					code: "0", 
					msg: "OK",
					post_list: post_list,
					count: count,
					next_page: next_page
				}
			end
		end

		#### 新建帖子 ####
		desc '发布评论'
		params do
			optional :user_id, type: String, desc: '用户名'
			optional :post_id, type: String, desc: '帖子ID'
			optional :content, type: String, desc: '评论内容'
		end
		post 'create_comment' do
			if params[:user_id].nil? || params[:post_id].nil? || params[:content].nil?
				{code: "400", msg: "参数错误"}
			else
				user = User.find(params[:user_id])
				comment = Comment.new
				comment.author_id = params[:user_id]
				comment.content = params[:content]
				comment.post_id = params[:post_id]
				comment.publish_status = 1
				comment.audit_status = 1
				comment.save
				comments = Comment.where(post_id: params[:post_id])
				comment_list = []
				comments.each do |co|
					a_user = User.find(co.author_id)
					comment_info = Hash.new
					comment_info["id"] = co.id
					comment_info["author_id"] = co.author_id
					comment_info["author_name"] = a_user.name
					comment_info["content"] = co.content
					comment_info["created_at"] = co.created_at.nil? ? "" : co.created_at.strftime("%Y-%m-%d %H:%M:%S")
					comment_list << comment_info
				end

				{
					code: "0", 
					msg: "评论成功",
					id: comment.id,
					content: comment.content,
					author_name: user.name,
					created_at: comment.created_at.nil? ? "" : comment.created_at.strftime("%Y-%m-%d %H:%M:%S"),
					comment_list: comment_list
				}
			end
		end


		#### 查询评论列表 ####
		desc '查询帖子评论列表'
		params do
			optional :user_id, type: String, desc: '用户名'
			optional :post_id, type: String, desc: '帖子ID'
			optional :page, type: String, desc: '页码'
		end
		get 'comments' do
			if params[:user_id].nil? || params[:post_id].nil?
				{code: "400", msg: "参数错误"}
			else
				if params[:page].nil?
					page = 1
				else
					page = params[:page]
				end
				comments = Comment.paginate(:page => page).order('created_at DESC')
				comment_list = []
				comments.each do |co|
					cuser = User.find(co.author_id)
					comment_info = Hash.new
					comment_info["id"] = co.id
					comment_info["author_id"] = co.author_id
					comment_info["author_name"] = cuser.name
					comment_info["content"] = co.content
					comment_info["created_at"] = co.created_at.nil? ? "" : co.created_at.strftime("%Y-%m-%d %H:%M:%S")
					comment_list << comment_info
				end
				count = comments.size
				next_page = params[:page].to_i + 1
				{
					code: "0", 
					msg: "OK",
					comment_list: comment_list,
					count: count,
					next_page: next_page
				}
			end
		end
	end
end